package ContentServer;


import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.alibaba.fastjson.JSONObject;

//parse weather data
public class DataParser {

    public static List<JSONObject> parseFile(String fileName) throws FileNotFoundException {
        JSONObject obj = new JSONObject();
        File file = new File(fileName);
        Scanner scanner = new Scanner(file);
        String content = scanner.useDelimiter("\\Z").next();
        content = removeMarkup(content);
        String[] lines = content.split("\\n");
        List<JSONObject> list = new ArrayList<>();
        for (String line : lines) {
            String[] parts = line.split(":");
            if ("id".equals(parts[0])) {
                if (obj.size() > 0) {
                    list.add(obj);
                }
                obj = new JSONObject();
            }
            if (parts.length > 1) {
                obj.put(parts[0], parts[1]);
            }
        }
        // add the last object
        if (obj.size() > 0) {
            list.add(obj);
        }
        // print all JSON object
        for (JSONObject jsonObj : list) {
            System.out.println(jsonObj.toString());
        }
        return list;
    }

    static String removeMarkup(String value) {
        return value.replaceAll("<.*?>", "");
    }

    public static void main(String[] args) throws FileNotFoundException {


        parseFile("C:\\Users\\AI\\Desktop\\test.txt");
    }
}
